<template>
  <div>
    <h2>会员等级录入</h2>
    <table>
      <tbody>
        <tr>
          <td>名称</td>
          <td>
            <input type="text" v-model="state.name" />
          </td>
        </tr>

        <tr>
          <td>密码</td>
          <td>
            <input type="password" v-model="state.pwd" />
          </td>
        </tr>

        <tr>
          <td>会员等级</td>
          <td>
            <select v-model="state.djPeople">
                <option value="">普通会员</option>
                <option value="白银级">白银级</option>
                <option value="黄金级">黄金级</option>
                <option value="钻石级">钻石级</option>
            </select>
          </td>
        </tr>

        <tr>
          <td>手机号</td>
          <td>
            <input type="text" v-model="state.phone" />
          </td>
        </tr>
        <tr>
          <td>邮件</td>
          <td>
            <input type="text" v-model="state.email" />
          </td>
        </tr>
        <tr>
          <td>时间</td>
          <td>
            <input type="date" v-model="state.createTime" />
          </td>
        </tr>
        <tr>
          <td></td>
          <td>
            <input type="button" value="增加" @click="add" />
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script setup lang="ts">
import axios from "axios";
import { reactive, toRefs, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();

let data = reactive({
  state: {
    name: "",
    pwd: "",
    djPeople: "",
    phone: "",
    email: "",
    createTime: "",
  },
});

let { state } = toRefs(data);



const add = () => {
  if (state.value.name == "") {
    alert("名称不能为空");
    return;
  }
  if (state.value.pwd == "") {
    alert("密码不能为空");
    return;
  }
  if (state.value.djPeople == "") {
    alert("等级不能为空");
    return;
  }
  if (state.value.phone == "") {
    alert("手机号不能为空");
    return;
  }
   var reg = /^1[3-9]\d{9}$/;
  if (reg.test(state.value.phone) == false) {
    alert("手机号格式不正确");
    return;
  }
  if (state.value.email == "") {
    alert("邮件不能为空");
    return;
  }
  if (state.value.createTime == "") {
    alert("时间不能为空");
    return;
  }
  let obj = {
    name: state.value.name,
    pwd: state.value.pwd,
    djPeople: state.value.djPeople,
    phone: state.value.phone,
    email: state.value.email,
    createTime: state.value.createTime,
  };
  axios({
    url: "/api/Deng/AddDeng",
    method: "post",
    data: obj,
  })
    .then((res) => {
      console.log(res);
      if (res.data.data == 1) {
        alert("添加成功");
        router.push("/ShowDeng");
      } else if (res.data.data == -1) {
        alert("名称重复");
        return;
      } else {
        alert("添加失败");
        return;
      }
    })
    .catch((err) => {
      console.log(err);
    });
};
</script>

<style scoped>
</style>